﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Player
{
    /// <summary>
    /// 乘骑中的船
    /// </summary>
    public Ship selectedShip{get{ return m_SelectedShip; } set { m_SelectedShip = value; } }
    public List<Ship> ships { get { return m_Ships; } }
    List<Ship> m_Ships = new List<Ship>();
    Ship m_SelectedShip = null;


    public void AddShip(Ship ship)
    {
        if (ship == null)
        {
            MessageShow.Instance.Warning("you have add a invaild ship.");
            return;
        }    

        m_Ships.Add(ship);
    }
    public JsonData Save()
    {
        JsonData root = new JsonData();
        JsonData shipsJson = new JsonData();
        shipsJson.SetJsonType(JsonType.Array);
        foreach(var i in m_Ships)
        {
            shipsJson.Add(i.Save());
        }
        root["Ships"] = shipsJson;


        return root;
    }
    public void Load(JsonData root)
    {
        var rootd = root.ValueAsObject();
        if(rootd.ContainsKey("Ships"))
        {
            var shipsJsond = rootd["Ships"].ValueAsArray();
            for (int i=0; i< shipsJsond.Count; ++i)
            {
                var ship = ShipsManager.Instance.LoadShip(shipsJsond[i]);
                if(ship)
                    m_Ships.Add(ship);
            }
        }
        //if (rootd.ContainsKey("Items"))
        //{
        //    var items = rootd["Items"].ValueAsArray();
        //    for (int i = 0; i < items.Count; ++i)
        //    {
        //        if (!items.Contains("TypeID"))
        //            continue;
        //        var typeID = items[i]["TypeID"].ValueAsInt();
        //        var item = ItemManager.Instance.createItem(typeID);
        //        m_Items.Add(item); 
        //    }
        //}
    }
}